package com.yuke.cloud.service.wmc.util;

/**
 * 自有业务打印电子面单，调用了快宝打印工具类
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.wmc.config.Printconfig;
import com.yuke.cloud.service.wmc.printHttpUtil.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class printUtil {

    public static Boolean printit(Map<String, Object> sender1, Map<String, Object> recipient1, String orderid, String waybill_no, String transid, List<Map<String, Object>> list,
                                  String pakgeName, String originName, String originCode, String routeCode, String sortation, String agentId) throws Exception {
        String host = "http://kop.kuaidihelp.com";
        String path = "/api";
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        Map<String, Object> print_data = new HashMap<String, Object>();//对应打印data
        Map<String, Object> data = new HashMap<String, Object>();//对应data
        bodys.put("app_id", Printconfig.userId);
        bodys.put("method", "cloud.print.do");
        //发送者
        Map<String, Object> sender = new HashMap<String, Object>();
        Map<String, Object> sendaddress = new HashMap<String, Object>();
        sendaddress.put("city",sender1.get("city"));
        sendaddress.put("province",sender1.get("province").toString());
        sendaddress.put("district", sender1.get("district").toString());
        sendaddress.put("detail",sender1.get("address").toString());
        sender.put("address", sendaddress);
        sender.put("mobile",sender1.get("mobile").toString());
        sender.put("name", "裕客网");
        //接受者
        Map<String, Object> recipient = new HashMap<String, Object>();
        Map<String, Object> recipientaddress = new HashMap<String, Object>();
        recipientaddress.put("city",recipient1.get("city").toString());
        recipientaddress.put("province", recipient1.get("province").toString());
        recipientaddress.put("district", recipient1.get("district").toString());
        recipientaddress.put("detail", recipient1.get("address").toString());
        recipient.put("address", recipientaddress);
        recipient.put("mobile", recipient1.get("mobile").toString());
        recipient.put("name", recipient1.get("name").toString());
        //路由什么的
        Map<String, Object> routing_info = new HashMap<String, Object>();
        Map<String, Object> consolidation = new HashMap<String, Object>();
        consolidation.put("name", pakgeName);
        Map<String, Object> origin = new HashMap<String, Object>();
        origin.put("code", originCode);
        origin.put("name", originName);
        Map<String, Object> sortationData = new HashMap<String, Object>();
        sortationData.put("name", sortation);
        routing_info.put("consolidation", consolidation);
        routing_info.put("origin", origin);
        routing_info.put("route_code", routeCode);
        routing_info.put("sortation", sortationData);
        //print_data
        print_data.put("recipient", recipient);
        print_data.put("tid",orderid);
        print_data.put("routing_info", routing_info);
        print_data.put("sender", sender);
        print_data.put("waybill_code",waybill_no);
        print_data.put("sequence", "1/1");
        //print_data.put("template_id","1/1");
        print_data.put("cp_code",transid);
        print_data.put("goods_name",list.get(0).get("name"));
        //print_data.put("seqtiduence","1/1");
        data.put("action", "print.json.cn");
//        data.put("agent_id", "D7DF49FC-16BB-441E-A11B-F168D96872F1");
        data.put("agent_id", agentId);
        data.put("print_type", "2");  // 1：底单 2：打印 3：底单和打印.(可选,默认3)
        //data.put("print_data", print_data.toString());
        data.put("print_data", print_data);
        bodys.put("data", JSON.toJSONString(data));
        String ts = System.currentTimeMillis() + "";
        bodys.put("ts", ts);
        String str = Printconfig.userId+"cloud.print.do" + ts + Printconfig.apikey;
        String sign = MD5.MD5(str, "utf-8");
        bodys.put("sign", sign);

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            JSONObject jsonObject1 = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
            if ("0".equals(jsonObject1.get("code").toString())) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }

}
